package cn.dnui_lf_705.dnui_16110100705.controller;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.List;

import cn.dnui_lf_705.dnui_16110100705.Adapter.FoodListAdapter;
import cn.dnui_lf_705.dnui_16110100705.R;
import cn.dnui_lf_705.dnui_16110100705.bean.CollectStatus;
import cn.dnui_lf_705.dnui_16110100705.bean.FoodInfo;
import cn.dnui_lf_705.dnui_16110100705.bean.ResultBean;
import cn.dnui_lf_705.dnui_16110100705.listener.OnRetrofitListener;
import cn.dnui_lf_705.dnui_16110100705.model.FoodModel;


public class FoodListActivity extends BaseActivity implements OnRetrofitListener<Object> {
    private RecyclerView recyclerView;
    private FoodListAdapter foodListAdapter;
    String shop_id;
    String phonenum;
    Menu menu;
    private String user_id;
    List<FoodInfo> list;
    SharedPreferences sharedPreferences;
    private ImageView imageView;
    String result = null;
    String shopname;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState);
        setContentView(R.layout.food_list);
        imageView = findViewById(R.id.collectstar);
        sharedPreferences = getSharedPreferences("yht",Context.MODE_PRIVATE);
        user_id = sharedPreferences.getString("user_id","");
         shop_id = getIntent().getStringExtra("shop_id");
        shopname = getIntent().getStringExtra("shop_name");
         phonenum = getIntent().getStringExtra("phonenum");
     //   if(result.equals("0")||result==null)
    //        menu.findItem(R.id.item_collect1).setTitle("未收藏");
    //    else
     //       menu.findItem(R.id.item_collect1).setTitle("已收藏");
        Toast.makeText(FoodListActivity.this,shop_id,Toast.LENGTH_SHORT).show();
        getFoodList(shop_id);
        initViews();
        initData();
        initEvents();

        if (setEnableSliding()) {
            ArActSlidLayout rootView = new ArActSlidLayout(this);
            rootView.bindActivity(this);//绑定需要窗口的布局：
        }
    }
    protected boolean setEnableSliding() {//默认返回是需要的，只需继承的时候重写次代码 true : 需要；false :不需要；
        return true;
    }






    //生成Menu的对象
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //创建菜单
        getMenuInflater().inflate(R.menu.collect_menu,menu);
        this.menu=menu;
        //菜单创建完毕后向服务器发送请求，判断是否已经收藏
        menu.findItem(R.id.item_collect).setTitle(shopname);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    void initViews() {
        recyclerView=findViewById(R.id.food_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
    }

    @Override
    void initEvents() {
        //处理【返回】和【收藏】按钮的点击事件
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                collect();
            }
        });
        //【返回】按钮调用finish（）关闭当前界面
        //【收藏】按钮调用collect（）方法
    }

    @Override
    void initData() {
        //通过Intent获取店铺名称

        //获取店铺的菜谱列表，调用getFoodList方法
        //getFoodList();
        //获取店铺的收藏状态，调用iscollected（）方法
        iscollect(user_id);
    }

//    public String getShop_id()
//    {
//        SharedPreferences sp;
//        sp = getSharedPreferences("shopInfo",Context.MODE_PRIVATE);
//        return sp.getString("shop_id","");
//    }
    //发送获取店铺的菜谱列表的网络请求
    private void getFoodList(String shop_id) {
        FoodModel foodModel = new FoodModel();
        foodModel.getFoodByShop(shop_id,this);
    }
    //发送获取店铺的收藏状态的网络请求
    private void iscollect(String user_id) {
            FoodModel foodModel = new FoodModel();
            foodModel.isCollected(user_id,shop_id,"0",this);
    }
    //发送对店铺的收藏信息进行修改的网络请求
    private void collect(){
        FoodModel foodModel = new FoodModel();
        foodModel.userCollectShop(user_id,shop_id,this);
    }

//    @Override
//    public void onsuccess(List<FoodInfo> foodInfo , int flag) {
//                foodListAdapter = new FoodListAdapter(FoodListActivity.this,foodInfo);
//                recyclerView.setAdapter(foodListAdapter);
//    }

//    @Override
//    public void onFailure(String string) {
//        showToast("网络连接失败1");
//    }List<FoodInfo> foodInfo,

    @Override
    public void onsuccess(Object o ,int flag) {

        switch (flag)
        {
            case 1:
                //获取店铺的菜谱列表
                foodListAdapter = new FoodListAdapter(FoodListActivity.this, (List) o,R.layout.food_item,phonenum);
                recyclerView.setAdapter(foodListAdapter);
                break;
            case 2:
                //获取店铺的收藏状态

                result = ((CollectStatus)o).getCollected().toString();
                if(result.equals("0"))
                {
                    imageView.setImageResource(android.R.drawable.btn_star_big_off);
              //
                }
                else {
                    imageView.setImageResource(android.R.drawable.btn_star_big_on);
              //
                }
                break;
            case 3:
                //对店铺的收藏信息进行修改
                if(((ResultBean)o).getSuccess().equals("1")){
                    if(result.equals("0")){
                        imageView.setImageResource(android.R.drawable.btn_star_big_on);
                        menu.findItem(R.id.item_collect1).setTitle("已收藏");
                        Toast.makeText(FoodListActivity.this,"收藏店铺成功",Toast.LENGTH_SHORT).show();
                        result="1";
                    }
                    else {
                        imageView.setImageResource(android.R.drawable.btn_star_big_off);
                        menu.findItem(R.id.item_collect1).setTitle("未收藏");
                        Toast.makeText(FoodListActivity.this,"取消店铺收藏成功",Toast.LENGTH_SHORT).show();
                        result="0";
                    }
                }
                else {
                    Toast.makeText(FoodListActivity.this,"操作失败",Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }


    @Override
    public void onFailure(String string) {
            showToast("网络连接失败");
    }

}
